$(document).ready(function () {
    layui.use(['form', 'layedit', 'laydate','upload'], function(){
        var upload = layui.upload;
        var form = layui.form
            ,layer = layui.layer
            ,layedit = layui.layedit
            ,laydate = layui.laydate;

        //日期时间范围
        var startTime = laydate.render({
            elem: '#startTime'
            ,min:0
            ,max:"2099-12-31"
            ,done: function(value, date){
                endTime.config.min ={
                    year:date.year,
                    month:date.month-1,
                    date: date.date
                };
            }
        });

        var endTime = laydate.render({
            elem: '#expiredTime' //指定元素
            ,min:0
            ,done: function (value, date) {
                startTime.config.max={
                    year:date.year,
                    month:date.month-1,
                    date: date.date
                }
            }
        });


        //二级联动
        form.on('select(catId)', function(data){
            $("#sonId").html("<option value=\"\">请选择</option>");
            form.render('select');
            $.ajax({
                type:'POST',
                url:'/getCatId',
                dataType:'json',
                data:"type="+data.value,
                success:function(data){
                    // console.log(data);
                    if (data[1].name != null){
                        $("#sonId").attr("name","catId");
                    }
                    if (data[1].itemName != null){
                        $("#sonId").attr("name","itemId");
                    }
                    var str= "";
                    for(var key in data){
                        if (data[key].name != null){
                            str += "<option value="+data[key].id+">"+data[key].name+"</option>";
                        }
                        if (data[key].itemName != null){
                            str += "<option value="+data[key].id+">"+data[key].itemName+"</option>";
                        }
                    }
                    $("#sonId").append(str);
                    form.render('select');
                }
            });

        });

        //选完文件后不自动上传
        upload.render({
            elem: '#test8'
            ,url:"https://httpbin.org/post"
            ,auto: false
            ,bindAction: '#carouselSub'
        });

        //提交按钮
        form.on('submit(carouselSub)', function(data){
            var fd = new FormData();
            var formData = new FormData($( "#carousel_form" )[0]);
            // console.log(data.elem) //被执行事件的元素DOM对象，一般为button对象
            // console.log(data.form) //被执行提交的form对象，一般在存在form标签时才会返回
            // console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
            // return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。

            $.ajax({
                url:"/carouselUpload",
                type:'post',
                dataType:'text',
                data:formData,
                async : false,
                contentType: false, //jax 中 contentType 设置为 false 是为了避免 JQuery 对其操作，从而失去分界符，而使服务器不能正常解析文件
                processData: false,//当设置为true的时候,jquery ajax 提交的时候不会序列化 data，而是直接使用data
                success:function (msg) {
                    alert(msg)
                    // if (msg == '"SUCCESS"'){
                    //     alert("提交成功")
                    //     layer.closeAll();
                    //     document.getElementById("coupon_install").reset();
                    // }
                }
            })

            return false;

        });



    });


})

